package com.mg.yygh.hosp.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.mg.yygh.hosp.repository.DepartmentRepository;
import com.mg.yygh.hosp.service.DepartmentService;
import com.mg.yygh.model.hosp.Department;
import com.mg.yygh.model.hosp.Hospital;
import com.mg.yygh.vo.hosp.DepartmentQueryVo;
import com.mg.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.print.DocFlavor;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author M_Dragon
 * @date 2022/8/31/031 11:47
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        // 将参数map转为对象
        String jsonString = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(jsonString, Department.class);
        Department targetDepartment =
                departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(targetDepartment != null){
            department.setId(targetDepartment.getId());
            department.setCreateTime(targetDepartment.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
        }
        departmentRepository.save(department);
    }

    @Override
    public Page<Department> getPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        Pageable pageable = PageRequest.of(page-1,limit);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Department> example = Example.of(department,exampleMatcher);

        Page<Department> departments = departmentRepository.findAll(example,pageable);


        return departments;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null){
//            repository.deleteById(department.getId());
            department.setIsDeleted(1);
            departmentRepository.save(department);
        }
    }

    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {
        // 声明最终结果数组
        List<DepartmentVo> result = new ArrayList<>();

        // 设置条件查询所有记录
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);

        // 将记录以大科室分组，生成map
        Map<String, List<Department>> listMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for(Map.Entry<String,List<Department>> map : listMap.entrySet()){
            // 大科室编号
            String bigCode = map.getKey();
            // 大科室对应所有小科室
            List<Department> departmentList1 = map.getValue();
            // 大科室名字
            String bigname = departmentList1.get(0).getBigname();

            // 封装大科室编号名字
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(bigname);

            // 封装小科室
            List<DepartmentVo> childList = new ArrayList<>();
            for(Department dept : departmentList1){
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(dept.getDepcode());
                departmentVo2.setDepname(dept.getDepname());
                childList.add(departmentVo2);
            }
            departmentVo.setChildren(childList);

            result.add(departmentVo);

        }

        return result;
    }

    @Override
    public String getDepname(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
